package com.example.xianzhi.controller.API;

import cn.hutool.json.JSONUtil;
import com.example.xianzhi.dao.RelationDao;
import com.example.xianzhi.dao.impl.RelationDaoImpl;
import com.example.xianzhi.service.RelationService;
import com.example.xianzhi.service.impl.RelationServiceImpl;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@YockMvcAnnotation.Controller("/apl_Relation")
public class Apl_RelationController {
    private RelationDao relationDao = new RelationDaoImpl();
    private RelationService relationService = new RelationServiceImpl();
    /**
     * 添加收藏
     * @param request
     * @param response
     * @param user_id //用户id
     * @param goods_id//商品id
     * @param need  // 是否想要(0不想要，1想要)
     * @throws Exception
     */

    @YockMvcAnnotation.RequestMapping("/Relation.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void Relation(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "user_id") int  user_id, //用户id
            @YockMvcAnnotation.RequestParam(name = "goods_id") int  goods_id,//商品id
            @YockMvcAnnotation.RequestParam(name = "need") int  need         // 是否想要(0不想要，1想要)
    ) throws Exception{
        //允许的请求的域是*，代表所有
        response.setHeader("Access-Control-Allow-Origin","*");
        //查看有没有收藏记录
        int sf=  relationDao.selectRelation(user_id,goods_id);
        //没有就加，有就改
        if(sf==0){
            relationDao.insertRelation(user_id,goods_id,need);
        }else {
            relationDao.updateRelation(user_id,goods_id,need);
        }
    }

    @YockMvcAnnotation.RequestMapping("/selectNeed.do")
    @YockMvcAnnotation.ResponseBody
    public String selectNeed(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "user_id") int  user_id, //用户id
            @YockMvcAnnotation.RequestParam(name = "goods_id") int  goods_id//商品id
    ) {
        //允许的请求的域是*，代表所有
        response.setHeader("Access-Control-Allow-Origin","*");
        Map<String, Object> map = new HashMap<>();
        response.setContentType("application/json;charset=utf-8");  //告诉前端返回来的是一个json对象
        int a = relationDao.selectNeed(user_id,goods_id);
        map.put("code",200);
        map.put("data",a);
        String jsonStr = JSONUtil.toJsonStr(map);
        return jsonStr;
    }
    @YockMvcAnnotation.RequestMapping("/deleerlaapi.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void deleerlaapi(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "relation_id") int relation_id
    ) throws Exception{
        //允许的请求的域是*，代表所有
        response.setHeader("Access-Control-Allow-Origin","*");

        //删除游览信息
        relationService.deleterela(relation_id);


    }

}
